package com.kaishengit.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kaishengit.entity.Document;
import com.kaishengit.util.DBHelp;
import com.kaishengit.util.RowMapper;

public class FileDao {
	DBHelp<Document> db = new DBHelp<Document>();
	
	public void save(String name,String fileName) {
		String sql = "insert into t_file (name,fileName) values (?,?) ";
		db.executeSQL(sql,name,fileName);
	}

	public List<Document> findAll() {
		String sql = "select id,name,fileName from t_file";
		return db.executeQueryForList(sql, new FileRowMapper());
	}
	
	public Document findByfileName(String fileName) {
		String sql = "SELECT id,`name`,fileName FROM t_file WHERE filename=?";
		return db.executeQueryForObject(sql, new FileRowMapper(), fileName);
	}

	private class FileRowMapper implements RowMapper<Document> {

		public Document mapRow(ResultSet rs) throws SQLException {
			Document f = new Document();
			f.setId(rs.getInt("id"));
			f.setName(rs.getString("name"));
			f.setFileName(rs.getString("fileName"));
			return f;
		}
		
	}
	
}
